package org.jxc.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.OutOrder;
import org.jxc.domain.OutOrderProduct;
import org.jxc.domain.Product;
import org.jxc.domain.ProductWarehouseStock;
import org.jxc.domain.SalesOrder;
import org.jxc.domain.SalesOrderProduct;
import org.jxc.domain.Warehouse;
import org.jxc.service.OutOrderProductService;
import org.jxc.service.OutOrderService;
import org.jxc.service.ProductService;
import org.jxc.service.SalesOrderProductService;
import org.jxc.service.SalesOrderService;
import org.jxc.service.StockService;
import org.jxc.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.domain.Dictionary;
import org.team4.exception.CRUDException;
import org.team4.service.DictionaryService;
import org.team4.util.LoginBean;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class SalesOrderAction extends GenericAction<SalesOrder> {
	private static final long serialVersionUID = -4665378081525232678L;
	@Autowired
	private SalesOrderService salesOrderService;
	@Autowired
	private SalesOrderProductService salesOrderProductService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private ProductService productService;
	@Autowired
	private StockService stockService;

	@Autowired
	private OutOrderService outOrderService;
	@Autowired
	private OutOrderProductService outOrderProductService;

	private List<Dictionary> billTypeList;

	private List<Warehouse> warehouseList;

	private List<OutOrderProduct> outOrderProducts;

	private SalesOrder salesOrder;

	private OutOrder order;

	private String salesOrderId;

	private List<Integer> saleAmountsList;

	private List<Double> salePriceList;

	private List<String> stockIdList;

	private List<String> productIdList;

	private List<SalesOrderProduct> itemList;

	private List<String> saleOrderProductIdList;
	private Integer auditState;

	public String listPagination() throws Exception {
		page = salesOrderService.listPagination(page);
		return SUCCESS;
	}

	public String choose() throws Exception {
		page = salesOrderService.listPaginationForChoose(page);
		return SUCCESS;
	}

	public String toSave() throws Exception {
		billTypeList = dictionaryService
				.findDictionaryBySuperId(JXCString.BILL_TYPE);
		warehouseList = warehouseService.findAll();
		return SUCCESS;
	}

	/**
	 * 新建
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		double total = 0.0;
		for (int i = 0; i < saleAmountsList.size(); i++) {
			total += saleAmountsList.get(i) * salePriceList.get(i);
		}
		salesOrder.setTotalPrice(total);
		salesOrderService.saveSaleOrder(salesOrder);
		for (int i = 0; i < saleAmountsList.size(); i++) {
			SalesOrderProduct sop = new SalesOrderProduct();
			sop.setAmounts(saleAmountsList.get(i));
			sop.setSalesPrice(salePriceList.get(i));
			sop.setLastPrice(salePriceList.get(i) * saleAmountsList.get(i));
			Product product = productService.findById(productIdList.get(i));
			sop.setProduct(product);
			sop.setSalesOrder(salesOrder);
			ProductWarehouseStock stock = stockService.findById(stockIdList
					.get(i));
			if (stock != null) {
				sop.setPrice(stock.getInOrderProduct().getInPrice());
				sop.setStock(stock);
			}
			salesOrderProductService.saveSalesOrderProduct(sop);
		}
		return SUCCESS;
	}

	public String toUpdate() throws Exception {
		billTypeList = dictionaryService
				.findDictionaryBySuperId(JXCString.BILL_TYPE);
		warehouseList = warehouseService.findAll();
		salesOrder = salesOrderService.findById(salesOrderId);
		itemList = salesOrderProductService
				.findAllSalesOrderProductBySalesOrderId(salesOrderId);
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		double total = 0.0;

		for (int i = 0; i < saleAmountsList.size(); i++) {
			total += saleAmountsList.get(i) * salePriceList.get(i);
		}
		salesOrder.setTotalPrice(total);
		salesOrderService.updateSalesOrder(salesOrder);
		List<SalesOrderProduct> list = salesOrderProductService
				.findAllSalesOrderProductBySalesOrderId(salesOrder.getId());
		List<String> deleteList = new ArrayList<String>();
		for (SalesOrderProduct sop : list) {
			if (!saleOrderProductIdList.contains(sop.getId())) {
				deleteList.add(sop.getId());
			}
		}
		salesOrderProductService.deleteSalesOrderProduct(deleteList);
		for (int i = 0; i < saleOrderProductIdList.size(); i++) {
			SalesOrderProduct sop = salesOrderProductService
					.findById(saleOrderProductIdList.get(i));
			sop.setAmounts(saleAmountsList.get(i));
			sop.setSalesPrice(salePriceList.get(i));
			sop.setLastPrice(salePriceList.get(i) * saleAmountsList.get(i));
			Product product = productService.findById(productIdList.get(i));
			sop.setProduct(product);
			sop.setSalesOrder(salesOrder);
			ProductWarehouseStock stock = stockService.findById(stockIdList
					.get(i));
			if (stock != null) {
				sop.setPrice(stock.getInOrderProduct().getInPrice());
				sop.setStock(stock);
			}
			salesOrderProductService.updateSalesOrderProduct(sop);
		}

		for (int i = saleOrderProductIdList.size(); i < saleAmountsList.size(); i++) {
			SalesOrderProduct sop = new SalesOrderProduct();
			sop.setAmounts(saleAmountsList.get(i));
			sop.setSalesPrice(salePriceList.get(i));
			sop.setLastPrice(salePriceList.get(i) * saleAmountsList.get(i));
			Product product = productService.findById(productIdList.get(i));
			sop.setProduct(product);
			sop.setSalesOrder(salesOrder);
			ProductWarehouseStock stock = stockService.findById(stockIdList
					.get(i));
			if (stock != null) {
				sop.setPrice(stock.getInOrderProduct().getInPrice());
				sop.setStock(stock);
			}
			salesOrderProductService.saveSalesOrderProduct(sop);
		}
		return SUCCESS;
	}

	public String toView() throws Exception {
		salesOrder = salesOrderService.findById(salesOrderId);
		itemList = salesOrderProductService
				.findAllSalesOrderProductBySalesOrderId(salesOrderId);
		return SUCCESS;
	}

	public String toOutWareHouse() throws Exception {
		// warehouseList = warehouseService.findAll();
		salesOrder = salesOrderService.findById(salesOrder.getId());
		itemList = salesOrderProductService
				.findAllSalesOrderProductBySalesOrderId(salesOrder.getId());
		order = outOrderService.findBySalesOrderId(salesOrder.getId());
		return SUCCESS;
	}

	/*
	 * 保存出库信息
	 */
	public String saveOut() throws CRUDException {

		if (order.getId() != null && !order.getId().isEmpty()) {
			String code = order.getCode();
			Date outStockDate = order.getOutStockDate();
			String remark = order.getRemark();
			order = outOrderService.findById(order.getId());
			order.setCode(code);
			order.setOutStockDate(outStockDate);
			order.setAuditState(JXCString.W_CHECK);
			order.setRemark(remark);
			outOrderService.update(order);

			// 更新先删除原来的明细
			outOrderProductService.delByOutOrderId(order.getId());
		} else {
			order.setCreator(LoginBean.getLoginBean().getUser());
			order.setCreateTime(new Date());
			order.setAuditor(LoginBean.getLoginBean().getUser().getDepartment()
					.getManager());
			outOrderService.save(order);
		}

		if (outOrderProducts != null && !outOrderProducts.isEmpty()) {
			for (int i = 0; i < outOrderProducts.size(); i++) {
				OutOrderProduct ooProduct = new OutOrderProduct();
				ooProduct = outOrderProducts.get(i);
				ooProduct.setOutOrder(order);
				outOrderProductService.save(ooProduct);
				// stockService.update(ooProduct);
			}
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				salesOrderService.deleteSalesOrder(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public String apply() throws Exception {
		salesOrder = salesOrderService.findById(salesOrderId);
		salesOrder.setAuditState(JXCString.APPLY);
		salesOrderService.updateSalesOrder(salesOrder);
		return SUCCESS;
	}

	public String audit() throws Exception {
		salesOrder = salesOrderService.findById(salesOrderId);
		salesOrder.setAuditState(auditState);
		salesOrderService.updateSalesOrder(salesOrder);
		return SUCCESS;
	}

	public SalesOrder getSalesOrder() {
		return salesOrder;
	}

	public void setSalesOrder(SalesOrder salesOrder) {
		this.salesOrder = salesOrder;
	}

	public List<Dictionary> getBillTypeList() {
		return billTypeList;
	}

	public void setBillTypeList(List<Dictionary> billTypeList) {
		this.billTypeList = billTypeList;
	}

	public List<Warehouse> getWarehouseList() {
		return warehouseList;
	}

	public void setWarehouseList(List<Warehouse> warehouseList) {
		this.warehouseList = warehouseList;
	}

	public List<Integer> getSaleAmountsList() {
		return saleAmountsList;
	}

	public void setSaleAmountsList(List<Integer> saleAmountsList) {
		this.saleAmountsList = saleAmountsList;
	}

	public List<Double> getSalePriceList() {
		return salePriceList;
	}

	public void setSalePriceList(List<Double> salePriceList) {
		this.salePriceList = salePriceList;
	}

	public List<String> getStockIdList() {
		return stockIdList;
	}

	public void setStockIdList(List<String> stockIdList) {
		this.stockIdList = stockIdList;
	}

	public List<String> getProductIdList() {
		return productIdList;
	}

	public void setProductIdList(List<String> productIdList) {
		this.productIdList = productIdList;
	}

	public String getSalesOrderId() {
		return salesOrderId;
	}

	public void setSalesOrderId(String salesOrderId) {
		this.salesOrderId = salesOrderId;
	}

	public List<SalesOrderProduct> getItemList() {
		return itemList;
	}

	public void setItemList(List<SalesOrderProduct> itemList) {
		this.itemList = itemList;
	}

	public List<String> getSaleOrderProductIdList() {
		return saleOrderProductIdList;
	}

	public void setSaleOrderProductIdList(List<String> saleOrderProductIdList) {
		this.saleOrderProductIdList = saleOrderProductIdList;
	}

	public List<OutOrderProduct> getOutOrderProducts() {
		return outOrderProducts;
	}

	public void setOutOrderProducts(List<OutOrderProduct> outOrderProducts) {
		this.outOrderProducts = outOrderProducts;
	}

	public OutOrder getOrder() {
		return order;
	}

	public void setOrder(OutOrder order) {
		this.order = order;
	}

	public Integer getAuditState() {
		return auditState;
	}

	public void setAuditState(Integer auditState) {
		this.auditState = auditState;
	}
}
